define(function(require, exports, module) {
    "use strict";
    var $ = require("$"),
        app = require("dist/application/app"),
        config = app.config,
        method = app.method;
	
	var $express = $(".express-hide",".form-validate");
	if($(".js_deliveery:checked").data("val") == 1){
		$express.addClass("hide");
		$("#svae_goods").data("save",false);
	}else{
		$express.removeClass("hide");
		$("#svae_goods").data("save",true);
	}

	$(".js_deliveery").length && $(".js_deliveery").change(function(e){
		if($(this).data("val") == 1){
			$express.addClass("hide");
			$("#svae_goods").data("save",false);
		}else{
			$express.removeClass("hide");
			$("#svae_goods").data("save",true);
		}
	})

	//删除货款
	$(document).on('click', '[data-toggle="ajaxPostRemove"]', function(e) {
        e.preventDefault();
        var $this = $(this),
            $remote = $this.data('remote') || $this.attr('href'),
            $data = $this.data("set"),
            $remove = $this.data('remove');
        $this.data("loadingText", '<i class="fa fa-spinner fa-spin"></i>');
        var $parent = $this.closest("div.carousel-caption");
        var done = function() {
            $this.button('loading');
            $.post($remote,$data).done(function(d) {
                $this.button('reset');
                if (config.issucceed(d)) {
                    if ($remove) {
                        var $el = $this.closest("ul.js_parent_ul");
                        $el.fadeOut(function() {
                            $el.remove();
                            if($parent.find('[data-toggle="ajaxPostRemove"]').length<=1){
                            	$parent.find('[data-toggle="ajaxPostRemove"]').addClass("hide");
                            }
                        });
                    };
                    var $msg = d.message || config.lang.removeSuccess;
                    if(typeof d.message != "string"){
                        window.mainFrame ? document.getElementById("mainFrame").src = d.url : window.location = d.url;
                    }else{
                        config.msg.suc($msg , d.url);
                    }
                    
                    d.callback&&eval(d.callback);
                } else {
                    config.msg.error(d.message || config.lang.removeError, d.url)
                }
            }).fail(function() {
                $this.button('reset');
                config.msg.info(config.lang.exception)
            });

        }
        done();
    });

	//单独发货
	$(document).on('click', '[data-toggle="ajaxPostReplace"]', function(e) {
        e.preventDefault();
        var $this = $(this),
            $remote = $this.data('remote') || $this.attr('href'),
            $data = $this.data("set"),
            $remove = $this.data('remove'),
            $p = $this.closest("div.carousel-caption");
        $this.data("loadingText", '<i class="fa fa-spinner fa-spin"></i>');
        var done = function() {
            $this.button('loading');
             $.ajax($remote, {
                type: "get",
                dataType: "html",
                data: $data
            }).done(function(result) {
            	$this.button('reset');
            	if ($remove) {
                    var $el = $this.closest("li.list-group-item");
                    var $parent = $this.closest("ul.js_parent_ul");
                    $el.fadeOut(function() {
                        $el.remove();
                        if($parent.find('[data-toggle="ajaxPostReplace"]').length <= 1){
                        	$parent.find('[data-toggle="ajaxPostReplace"]').addClass("hide");
                        }
                    });
                };
                var _html = $(result).append($el.clone());
                	_html.find('[data-toggle="ajaxPostReplace"]').addClass("hide");
            	$p.append2(_html.html(), function() {

            	})

            }).fail(function() {
                $this.button('reset');
                config.msg.info(config.lang.exception)
            });
        }
        done();
    });
	
	
	//批量填充
	$(document).on("click",".js_batch_ed",function(){
		$("#js_numbers_modal").modal();
		$("#js_numbers_modal").find("form")[0].reset();
	})


	//填充确定
	$(document).on("click","#batch_save",function(){
		if($(this).closest("form").valid()){
			resetNameFn($("#odd_numbers").val());
			$("#js_numbers_modal").modal("hide");
		}
	})

	//保存
	$(document).on("click","#svae_goods",function(){
		if($(this).data("save")){
			$("div#carousel-generic").find("div.js_item").each(function(i){
				if($(this).find('[data-nums="true"]').val() == ""){
					indexFn($(this).find('[data-nums="true"]'));
					config.msg.error("请填写快递单号", null);
					return false;
				}
			})
		}
	})

	var resetNameFn = function(val){
		var $parent = $("#carousel-generic").find("div.js_item").filter(".active"),
			$zeroLength = [],
			$val = parseInt(val),
			$str = val.toString();
		for(i=0;i<$str.length;i++){
			if($str.charAt(i) != 0){
				break;
			}
			$zeroLength.push($str.charAt(i));
		}
		
		$parent.find('[data-nums="true"]').each(function(i){
			$(this).val($zeroLength.join("")+($val++));
		})
	}

	
	var indexFn = function(errorElement){
		if(!errorElement.length) return;
		var index = $(errorElement).closest("div.js_item ").data("item");
		$('#carousel-generic').carousel(index);
    }
})